package app;

//import org.apache.log4j.Logger;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Iterator;

//import org.apache.log4j.BasicConfigurator;

import color.Color;
import color.Lab;

import cluster.Cluster;
import cluster.Duster;
import cluster.Duster.Relation;

import app.gui.Drawable;
import app.gui.Interactive;
import app.gui.MouseDragger;
import app.gui.NumberScroller;

import processing.core.PApplet;


public class ClusterApp extends PApplet implements ProgressListener{
	/**
	 * Logger for this class
	 */
	//private static final Logger logger = Logger.getLogger(ClusterApp.class);

	ClusterInterface intrface;
	XMLParser xmlParser;
	PixelReader pixelReader;
	Processor proc;
	ArrayList<Color> colors;
	public ArrayList<Cluster<Color>> clusters;
	ArrayList<Drawable> graphics;
	public ClusterDrawer drawer;
	NumberScroller[] scrollers;
	static public void main(String args[]) {
		//BasicConfigurator.configure();
		
		PApplet.main(new String[] { 
			//"--present",
			//"--bgcolor=#F00333",
			//"--present-stop-color=#000000",
			"app.ClusterApp" 
		});
	}
	String fileName = "autumn";
	String extension = ".jpg";
	public void setup(){
		
		size(1440, 750);
		framerate(20);
		drawer = new ClusterDrawer(0, 0, width, height - 300, null);
		clusters = new ArrayList<Cluster<Color>>();
		proc = new Processor(this, 0, 0, width, height);
		proc.getColors(fileName+extension, 8, 300);
		//intrface = new ClusterInterface(this);
		interactive = new ArrayList<Interactive>();
		MouseDragger drag = new MouseDragger(this);
		interactive.add(drag);
		scrollers = new NumberScroller[5];
		int size = 40;
		float space = 0.4f;
		int x = 100;
		int y = height - 100;
		for(int i = 0; i < 5; i++){
			scrollers[i] = new NumberScroller(this, size, x, y, new Color(new Lab(75, -50, 50)), -1, 200);
			x += (int)(2f * (float)size + (float)size * space);
			interactive.add(scrollers[i]);
		}
		
		
	}
	
	public void draw(){
		background(90, 95, 95);
		//intrface.updateVals();
		if(proc.busy) proc.progressBar.draw(this);
		else if(drawer.root != null){
			drawer.draw(this);
		}
		if(!proc.busy) {
			for(int i = 0; i < 5; i++) scrollers[i].draw(this);
		}
	}
	float progress = 0;
	public void progress(String task, float progress) {
		this.progress = progress;
		
	}
	
//	public void mousePressed(){
//		if(mouseButton == RIGHT){
//			intrface.openClose(mouseX, mouseY);
//		}
//	}
	ArrayList<Interactive> interactive;
	
	public void mousePressed(){
		Iterator<Interactive> i = interactive.iterator();
		while(i.hasNext()) i.next().mousePressed(this);
	}
	public void mouseReleased(){
		Iterator<Interactive> i = interactive.iterator();
		while(i.hasNext()) i.next().mouseReleased(this);
	}
	public void mouseDragged(){
		Iterator<Interactive> i = interactive.iterator();
		while(i.hasNext()) i.next().mouseDragged(this);
	}
	
	public void mouseMoved(){
		Iterator<Interactive> i = interactive.iterator();
		while(i.hasNext()) i.next().mouseMoved(this);
	}
	
	public void keyPressed(){
		Iterator<Interactive> i = interactive.iterator();
		while(i.hasNext()) i.next().keyPressed(this);
		if(key == 'c'){
//			if (logger.isDebugEnabled()) {
//				logger.debug("keyPressed() - recluster" + scrollers[0].val + " " + scrollers[4].val);
//				float[] r = new float[scrollers.length];
//				for (int j = 0; j < r.length; j++) {
//					r[j] = scrollers[j].val;
//				}
//				proc.cluster(proc.colors, r);
//			}
		}
		if(key == 'x'){
			XMLParser.writeXML(this, clusters.get(clusters.size()-1).root, fileName+clusters.size()+".xml");
			this.saveFrame(fileName+clusters.size()+".tga");
		}
	}

	public void processDone(String p) {
//		if (logger.isDebugEnabled()) {
//			logger.debug("processDone(String)" + p);
//		}
		if(p == "getColors"){
			colors = proc.colors;
			proc.cluster(proc.colors, new float[]{110, 7.4f, 2f});
		}
		if(p == "cluster"){
			clusters.add(proc.cluster);
			drawer.root = proc.cluster.root;
			drawer.initByMaxDev();
			
		}
		
	}
	
	public void actionPerformed(ActionEvent e){
		if(e.getActionCommand().equals("cluster")){
			float[] r = new float[intrface.l.length];
			for(int i = 0; i < r.length; i++) r[i] = intrface.l[i].value;
			proc.cluster(colors, r);
			intrface.gui._visible = false;
		}
	}
	
	

}
